<?php
class ModelInstall extends Model {

    public function mysql($data) {

        $pdo = new database($data['db_driver'], $data['db_host'], $data['db_user'], $data['db_password'], $data['db_name']);	

        foreach (glob($this->load->get_path('dir_app') . "struct/*.sql") as $filename) {	
            $content = file_get_contents($filename);
            $content = str_replace('prefix_', $data['db_prefix'], $content);
            try{
                $query = $pdo->prepare($content);
                $query->execute();
            } catch (PDOException $e){
                echo $e->getMessage();
                echo $filename;
                exit();
            }
            unset($content);
            unset($query);
        }
 
        //Add User
        $perm = array(
            'access' => array(
                "setting/setting",
                "setting/store",
                "user/user", 
                "user/user_permission"
            ), 
            'modify'  => array(
                "user/user", 
                "user/user_permission"
            ) 
        );

        $query = $pdo->prepare("INSERT INTO " . $data['db_prefix'] . "user_group
            (user_group_id, name, permission) 
            VALUES (1, 'Top Administrator', :permission)");

        $perm = serialize($perm);
        $query->execute(array(':permission' => $perm));
        unset($query, $perm);

        $salt = substr(md5(uniqid(rand(), true)), 0, 9);
        $password = sha1($salt . sha1($salt . sha1($data['password'])));

        $user_query = $pdo->prepare("INSERT INTO " . $data['db_prefix'] . "user SET 
            user_id           = 1,
            username          = :username,
            email             = :email, 
            salt              = :salt, 
            password          = :password, 
            user_group_id     = 1, 
            status            = '1', 
            date_added        = NOW()
            ");

        $user_query->execute(array(
            ':username' =>  $data['username'], 
            ':salt'     =>  $salt, 
            ':password' =>  $password, 
            ':email'    =>  $data['email'], 
        ));

        //Add minimal data to database 
        //This is the correct way to loop over the directory. 
        foreach (glob($this->load->get_path('dir_app') . "data/*.sql") as $filename) {	
            $content = file_get_contents($filename);
            $content = str_replace('prefix_', $data['db_prefix'], $content);
            try{
                $query = $pdo->prepare($content);
                $query->execute();
            } catch (PDOException $e){
                echo $e->getMessage();
                echo $filename;
                exit();
            }
            unset($content);
        }

        unset($query);

        //Update Settings 
        $update_query = $pdo->prepare("UPDATE " . $data['db_prefix'] . "setting SET 
            s_value     = :value,
            serialized      = :serialized 
            WHERE   app_id      = :app_id
            AND     s_key       = :key
            AND     store_id    = :store_id");

        $update_query->execute(array(    
            ':value'        => $data['email'],
            ':app_id'       => 'config',   
            ':key'          => 'config_email',
            ':store_id'     => 0,
            ':serialized'   => 0));

        $update_query->execute(array(    
            ':value'        => md5(mt_rand()),
            ':app_id'       => 'config',       
            ':key'          => 'config_encryption', 
            ':store_id'     => 0,
            ':serialized'   => 0));

        $update_query->execute(array(    
            ':value'        => HTTP_SERVER,
            ':app_id'       => 'config',       
            ':key'          => 'http_server', 
            ':store_id'     => 0,
            ':serialized'   => 0));

       $update_query->execute(array(    
            ':value'        => HTTPS_SERVER,
            ':app_id'       => 'config',       
            ':key'          => 'https_server', 
            ':store_id'     => 0,
            ':serialized'   => 0));

    }
}
?>
